package previous.Test_0308;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
class circle{
    int num;
    int count;
    public circle(int num){
        this.num=num;
        this.count=Count(num);
    }
    public  int Count(int num){
        int c=0;
        while(num!=0){
            int t=num%10;
            if(t==1 || t==2 || t==3 || t==5 || t==7){
                c+=0;
            }else if(t==8){
                c+=2;
            }else {
                c+=1;
            }
            num/=10;
        }
        return c;
    }
}
public class Blue_bridge1 {
    //封闭图形个数
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n=scan.nextInt();
        List<circle> a=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int tmp=scan.nextInt();
            a.add(new circle(tmp));
        }
        Collections.sort(a, new Comparator<circle>() {
            @Override
            public int compare(circle o1, circle o2) {
                if(o1.count!=o2.count) return o1.count- o2.count;
                else return o1.num-o2.num;
            }
        });
        for (circle c:a) {
            System.out.print(c.num+" ");
        }
        scan.close();
    }
    //计算封闭个数

}
